package com.ww.word.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

import com.ww.word.R;
import com.ww.word.entity.Word;
import com.ww.word.ui.StudyActivity;

/**
 * @since 
 * @version 2013-7-11
 * @author wangwei
 */
public class StudyAdapter extends PagerAdapter {
	private List<Word> list;
	private Activity context;
	private int total;

	public StudyAdapter(Activity context, List<Word> list) {
		this.context = context;
		this.list = list;
		total = list.size();
	}

	@Override
	public int getCount() {
		return total;
	}

	@Override
	public boolean isViewFromObject(View view, Object obj) {
		return view == (View) obj;
	}

	@Override
	public Object instantiateItem(ViewGroup container, final int position) {

		if (position < list.size() - 1) {
			View parent = getVieww(position);
			((ViewPager) container).addView(parent, 0);
			return parent;
		} else {
			View v = LayoutInflater.from(context).inflate(R.layout.activity_turn, null);
			Button txt4 = (Button) v.findViewById(R.id.turn);
			txt4.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent i = new Intent(context, StudyActivity.class);
					context.startActivity(i);
					context.finish();
				}
			});
			
//			Button turn2 = (Button) v.findViewById(R.id.turn2);
//			turn2.setText("再来一遍");
//			turn2.setOnClickListener(new OnClickListener() {
//				@Override
//				public void onClick(View v) {
//					ReviewActivity.ddd();
//				}
//			});
			
			Button turn3 = (Button) v.findViewById(R.id.turn3);
			turn3.setVisibility(View.GONE);
			
			((ViewPager) container).addView(v, 0);
			return v;
		}
	

	}

	private View getVieww(final int position) {
		WordAdapter w = new WordAdapter(context, list.get(position));
		return w.getReviewView();
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		container.removeView((View) object);
	}

}
